package com.like.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.like.adapter.VideoCourseAdapter;
import com.like.entity.CourseInfo;
import com.like.entity.DocListResp;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.GetCourseListReq;
import com.like.entity.GetCourseListResp;
import com.like.hospitalworld.R;
import com.like.hospitalworld.VideoCourseDetailActivity;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.RetrofitUtil;
import com.like.network.WindowService;
import com.like.utils.Constants;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.fragment_video_course)
public class VideoCourseFragment extends BaseFragment {

    @ViewInject(R.id.video_course_list)
    private PullToRefreshListView mVideoCourseList;

    private VideoCourseAdapter mAdapter;
    private WindowService mWindowService;
    private int mCurrentPage = 0;
    private int mPageCount = 15;
    private DocListResp.Doctor mDoctor;
    private int mTypeId;

    public void setExpert(DocListResp.Doctor doctor) {
        this.mDoctor = doctor;
    }

    public void setTypeId(int typeId) {
        this.mTypeId = typeId;
    }

    @Override
    protected void onCreateView() {
        super.onCreateView();
        initList();
        mWindowService = RetrofitUtil.getService(WindowService.class);
        getCourseList(false);
    }

    private void initList() {
        mVideoCourseList.setMode(PullToRefreshBase.Mode.BOTH);
        mVideoCourseList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage = 0;
                getCourseList(false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage += 1;
                getCourseList(true);
            }
        });
    }

    private void getCourseList(final boolean append) {
        GetCourseListReq req = new GetCourseListReq(m.getCurrentDep().id, mDoctor.id, mCurrentPage, mPageCount);
        Call<GeneralResponse<GetCourseListResp>> call = mWindowService.getCourseList(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<GetCourseListResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<GetCourseListResp>> call,
                                   Response<GeneralResponse<GetCourseListResp>> response) {
                mVideoCourseList.onRefreshComplete();
                if (!response.isSuccessful())
                    return;
                GeneralResponse<GetCourseListResp> resp = response.body();
                GetCourseListResp courseListResp = resp.data;
                List<CourseInfo> courseInfos = courseListResp.informationList;
                List<CourseInfo> infos = getInfos(courseInfos);
                if (mAdapter == null) {
                    mAdapter = new VideoCourseAdapter(m, infos);
                    mVideoCourseList.setAdapter(mAdapter);
                } else
                    mAdapter.update(infos, append);
            }

            @Override
            public void onFailure(Call<GeneralResponse<GetCourseListResp>> call, Throwable t) {
                t.printStackTrace();
                mVideoCourseList.onRefreshComplete();
            }
        });
    }

    private List<CourseInfo> getInfos(List<CourseInfo> infos) {
        List<CourseInfo> resultInfos = new ArrayList<>();
        for (CourseInfo info : infos) {
            if (info.typeId == mTypeId)
                resultInfos.add(info);
        }
        return resultInfos;
    }

    @Event(value = R.id.video_course_list, type = AdapterView.OnItemClickListener.class)
    private void courseClick(AdapterView<?> parent, View view, int position, long id) {
        position -= 1;
        CourseInfo courseInfo = (CourseInfo) mAdapter.getItem(position);
        EventBus.getDefault().postSticky(new EventWrapper(courseInfo, VideoCourseDetailActivity.class, VideoCourseDetailActivity.REQUEST_VIDEO_COURSE_INFO));
        Intent intent = new Intent(m.mContext, VideoCourseDetailActivity.class);
        startActivity(intent);
    }
}
